package com.geek.framework.study.config.servlet.initializer;

import com.geek.framework.study.config.microprofile.converter.StringConvert;
import com.geek.framework.study.config.microprofile.source.ServletContextConfigSource;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Set;

public class ServletContextInitializer implements ServletContainerInitializer {


    @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        ConfigProviderResolver configProviderResolver = ConfigProviderResolver.instance();

        Config config = configProviderResolver.getBuilder()
                .addDefaultSources()
                .addDiscoveredConverters()
                .withSources(new ServletContextConfigSource(ctx))
                .withConverters(new StringConvert())
                .build();
        configProviderResolver.registerConfig(config, getClass().getClassLoader());

    }
}
